package com.young.fs.service.impl;

import com.young.common.core.dal.service.IDataAccessService;
import com.young.common.util.CommonUtil;
import com.young.common.util.Page;
import com.young.fs.model.EFileType;
import com.young.fs.model.FileInfo;
import com.young.fs.model.FileStatisticsOnType;
import com.young.fs.service.IFileInfoService;
import com.young.fs.util.FsConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.*;

/**
 * @description
 * @authhor yangwei
 * @create 2018/10/8 0008
 */
@Service("fsFileInfoService")
public class FileInfoServiceImpl implements IFileInfoService{
    @Resource(name = "dataAccessService")
    IDataAccessService dataAccessService;//数据层服务

    private static final Logger logger = LoggerFactory.getLogger(FileInfoServiceImpl.class);

    @Override
    public List<FileInfo> getFileInfoList(FileInfo fileInfo) {
        return dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFileInfo", fileInfo);
    }

    //查询子文件夹信息
    @Override
    public List<FileInfo> getSubFolderList(String folderId) {
        return dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFolderListByFolderId", folderId);
    }

    @Override
    public FileInfo getFileInfo(String id) {
        return (FileInfo)dataAccessService.getObject(FsConstants.MAPPER.FS_FILEINFO + ".getFileInfoById", id);
    }

    @Override
    public List<FileInfo> getFolderTree(FileInfo fileInfo) {
        if(fileInfo == null){
            fileInfo = new FileInfo();
        }
        fileInfo.setIsFile("0");//0表示文件夹
        return this.getFileInfoList(fileInfo);
    }

    @Override
    public List<FileInfo> getFileInfoPage(FileInfo fileInfo, Page page) {
        return dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFileInfo", fileInfo, page);
    }

    @Override
    public int updateFileInfo(FileInfo fileInfo) {
        if (fileInfo.getUpdateTime() == null){//设置更新时间
            fileInfo.setUpdateTime(new Date());
        }
        return dataAccessService.update(FsConstants.MAPPER.FS_FILEINFO + ".updateFileInfo", fileInfo);
    }

    @Override
    public int deleteFileInfo(String[] idsArr) {
        return dataAccessService.delete(FsConstants.MAPPER.FS_FILEINFO + ".deleteFileInfo", idsArr);
    }

    @Override
    public int deleteFileInfoByFolder(String folderId) {
        int count = 0;
        //查询文件夹下的所有文件夹
        List<FileInfo> childList = this.getSubFolderList(folderId);//dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFolderListByFolderId", folderId);
        if (childList != null && childList.size() > 0){
            for (FileInfo file : childList){
                //计算该子文件夹下的文件数
                int num = this.countFileNumByFolderId(file.getId());//(Integer)dataAccessService.getObject(FsConstants.MAPPER.FS_FILEINFO + ".countFileNumByFolderId", file.getId());
                if (num > 0){
                    count += deleteFileInfoByFolder(file.getId());//清空它,递归
                }
            }
        }
        //子文件夹全部清空后,清空文件夹下的直属文件信息
        count += dataAccessService.delete(FsConstants.MAPPER.FS_FILEINFO + ".deleteFileInfoByFolder", folderId);
        logger.info("[清空文件夹信息] 文件夹[{}]下的信息已清空,总计删除文件信息(含文件夹){}个!", folderId, count);
        return count;
    }

    @Override
    public int insertFileInfo(FileInfo fileInfo) {
        if (fileInfo.getCreateTime() == null){//设置插入时间
            fileInfo.setCreateTime(new Date());
            fileInfo.setUpdateTime(new Date());
        }
        if (fileInfo.getId() == null){//设置编号
            fileInfo.setId(CommonUtil.getUUID());
        }
        return dataAccessService.insert(FsConstants.MAPPER.FS_FILEINFO + ".insertFileInfo", fileInfo);
    }

    @Override
    public List<FileInfo> getFileInfoByFolder(String folderId) {
        return dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFileInfoByFolder", folderId);
    }

    @Override
    public List<FileStatisticsOnType> statisFileNum(FileInfo fileInfo) {
        List<Map<String, Object>> list = dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".countFileNumByType", fileInfo);
        //list转map
        Map<String, Integer> map = new HashMap<String, Integer>();
        for (Map<String, Object> m : list){
            map.put(m.get("STYPE")+"", Integer.parseInt(m.get("N")+""));
        }
        //文件类型集合
        List<Map<String, String>> list2 = this.getFileTypeList();
        List<FileStatisticsOnType> resList = new ArrayList<FileStatisticsOnType>();
        for (Map<String, String> m : list2){
            FileStatisticsOnType one = new FileStatisticsOnType();
            one.setType(m.get("key"));
            one.setTypeRemark(m.get("value"));
            one.setNum(map.containsKey(m.get("key")) ? map.get(m.get("key")) : 0);
            resList.add(one);
        }
        Collections.sort(resList, new Comparator<FileStatisticsOnType>() {
            @Override
            public int compare(FileStatisticsOnType o1, FileStatisticsOnType o2) {
                if (o1.getNum() > o2.getNum()){
                    return 1;
                }
                return 0;
            }
        });
        return resList;
    }

    @Override
    public int countFileNumByFolderId(String folderId) {
        return (Integer)dataAccessService.getObject(FsConstants.MAPPER.FS_FILEINFO + ".countFileNumByFolderId", folderId);
    }

    /**
     * 根据业务编号查询文件信息
     *
     * @param businessCode 业务编号
     * @return
     */
    @Override
    public List<FileInfo> getFileInfoListByBusinessCode(String businessCode) {
        return dataAccessService.getList(FsConstants.MAPPER.FS_FILEINFO + ".getFileInfoListByBusinessCode", businessCode);
    }

    @Override
    public List<Map<String, String>> getFileTypeList() {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        Class cls = EFileType.class;
        Field[] fields = cls.getDeclaredFields();
        for(Field field : fields){
            Map<String,String> map = new HashMap<String, String>();
            EFileType.Remark remark = field.getAnnotation(EFileType.Remark.class);
            if(remark != null){
                map.put("key", field.getName());
                map.put("value", remark.value());
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public FileInfo getFolderInfo(String id) {
        return (FileInfo)dataAccessService.getObject(FsConstants.MAPPER.FS_FILEINFO + ".getFolderInfoById", id);
    }
}
